package com.wyvoid.forum.web.action.admin.usergroup;

import net.sourceforge.stripes.action.*;
import net.sourceforge.stripes.integration.spring.SpringBean;
import com.wyvoid.forum.web.action.admin.BaseAdminAction;
import com.wyvoid.forum.service.IUserGroupService;
import com.wyvoid.forum.service.IAdminGroupService;
import com.wyvoid.forum.domain.UserGroup;
import com.wyvoid.forum.domain.AdminGroup;

import java.util.List;

/**
 * User: cjp
 * Date: 2007-7-2
 * Time: 21:10:06
 */
@UrlBinding(value = "/admin/adminGroupManage.do")
public class AdminGroupManageAction extends BaseAdminAction {
    long[] adminGroupId;
    private UserGroup userGroup;
    String[] permissionsStr;
    private AdminGroup adminGroup;
    long groupId;

    public long getGroupId() {
        return groupId;
    }

    public void setGroupId(long groupId) {
        this.groupId = groupId;
    }

    @SpringBean
    private IUserGroupService userGroupService;
    private List<UserGroup> adminUserGroups;


    public void setUserGroupService(IUserGroupService userGroupService) {
        this.userGroupService = userGroupService;
    }

    public List<UserGroup> getAdminUserGroups() {
        return adminUserGroups;
    }


    public long[] getAdminGroupId() {
        return adminGroupId;
    }

    public void setAdminGroupId(long[] adminGroupId) {
        this.adminGroupId = adminGroupId;
    }

    public UserGroup getUserGroup() {
        return userGroup;
    }

    public void setUserGroup(UserGroup userGroup) {
        this.userGroup = userGroup;
    }

    @SpringBean
    private IAdminGroupService adminGroupService;

    public IAdminGroupService getAdminGroupService() {
        return adminGroupService;
    }

    @SpringBean
    public void setAdminGroupService(IAdminGroupService adminGroupService) {
        this.adminGroupService = adminGroupService;
    }

    public AdminGroup getAdminGroup() {
        return adminGroup;
    }

    public void setAdminGroup(AdminGroup adminGroup) {
        this.adminGroup = adminGroup;
    }

    public String[] getPermissionsStr() {
        return permissionsStr;
    }

    public void setPermissionsStr(String[] permissionsStr) {
        this.permissionsStr = permissionsStr;
    }
    
    @DefaultHandler
    public Resolution viewAdminGroups() {
        adminUserGroups = userGroupService.getAdminGroups();
        return getAdminResolution("usergroup/viewAdminGroups");
    }

    public Resolution normalAdminGroupForm() {
        for (int i = 0; i < adminGroupId.length; i++) {
            long groupId;
            groupId = adminGroupId[i];
            if (groupId > 0) {
                String groupTitle = context.getRequest().getParameter("group_title[" + groupId + "]");
                String starsStr = context.getRequest().getParameter("group_stars[" + groupId + "]");
                int stars = 0;
                if (starsStr != null) {
                    stars = Integer.parseInt(starsStr);
                }
                userGroup = userGroupService.getUserGroupById(groupId);
                userGroup.setType(1);
                userGroup.setTitle(groupTitle);
                userGroup.setStars(stars);
                userGroupService.updateUserGroup(userGroup);
            }
        }
         return new RedirectResolution("/forum/admin/userGroupManage.do", false);
    }

    @DontValidate
      public Resolution viewAdminGroupDroit() {
//        SessionUser user = getContext().getUser();
//         if(user!=null){
            adminGroup = adminGroupService.getAdminGroup(groupId);
            return getAdminResolution("usergroup/viewAdminGroupDroit");
//        }else{
//                   return null;
//        }
    }

     @DontValidate
     public Resolution setDetaiPurview() {
        String permissions = "|";
         String str=null;
          for (int i = 0; i < permissionsStr.length; i++) {
            str=context.getRequest().getParameter(permissionsStr[i]);
            System.out.println(str);
            if(!str.equals("0")){
            permissions+=str+"|";
            }
        }
        adminGroupService.updateAdminGroup(groupId,permissions);
        return new RedirectResolution("/forum/admin/adminGroupManage.do?action=viewAdminGroupDroit&id="+groupId, false);
    }
}
